<?
include_once("class/ProductInfo.php");
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));

$p=new ProductInfo($s,$pid);
$p_info=$p->pInfo();
$begintime=$p->getMinValidTime();
if($begintime==-1) $begintime=date("Y-m-d");
$storageXml=$s->Dynamic_Storage_Check($p_info['tid'],2,NUll,$p_info['storage_open']);
$storageXml=simplexml_load_string($storageXml);
$sales=$storageXml->Rec->UUXstr;
?>
<link type="text/css" rel="stylesheet" href="css/ui/storageCal.css" media="screen" />
<script src="js/Calendar.js" type="text/javascript"></script>
<style>
#storageCal{float:left}
#config{float:right;width:160px;}
#config table{width:100%;}
#config td,#config th{ border:1px solid #eee; line-height:30px;}
#config th{ text-align:right; font-weight:normal;}
#config .textT{width:60px;height:35px;line-height:35px;}
#config .inputTime{width:80px;height:35px;line-height:35px;}
#config td{padding-left:5px;}
#config table .btn{padding:2px 10px; cursor:pointer; line-height:20px}
.total_storage{margin-top:20px;}
.total_storage .btn{text-align:center;}
.total_storage p{line-height:20px; margin-top:5px;}

.clear{clear:both;}

</style>
	<div class="clay">
		<input type="hidden" id="tid" value="<?=$p_info['tid']?>"/>
		<input type="hidden" id="pid" value="<?=$pid?>"/>
		<input type="hidden" id="begintime" value="<?=$begintime?>"/>
		<h3><?=$p_info['ltitle'].$p_info['ttitle']?>-价格库存设置</h3>
		<div id="storageCal"> 
		</div>
		<div id="config">
			<form class="storage">
				<table>
					<tr>
						<th>供货价：</th>
						<td colspan="2"><input type="text" class="textT" id="sprice" name="daystorage" /></td>
					</tr>
					<tr>
						<th>零售价：</th>
						<td colspan="2"><input type="text" class="textT" id="lprice" name="daystorage"/></td>
					</tr>
					<tr>
						<th>日库存：</th>
						<td colspan="2"><input type="text" class="textT" id="daystorage" name="daystorage" /></td>
					</tr>
                    <?php if( $p_info['sync_status']==2 && $p_info['sync_id'] ):?>
                    <tr><td colspan="2">同步产品禁止编辑</td></tr>
                    <?php else:?>
					<tr>
						<th><input type="submit" class="btn" id="configBtn" value="设置"/></th>
						<td><!--<input type="reset" class="btn" value="重置"/>--></td>
						<td><input type="button" class="btn" id="delBtn" value="还原默认"/></td>
					</tr>
                    <?php endif;?>
				</table>
				<!-- <p>
					1,日库存留空，-1表示库存不限
				</p> -->
				<p>
					0表示日库存为空，-1表示日库存不限
				</p>
			</form>
			<form class="total_storage">
				<table>
					<tr>
						<th><input type="checkbox" id="applyCheck" <?=$p_info['storage']==-1?"":"checked"?> />&nbsp;&nbsp;&nbsp;</th>
						<td><span class="leaderTxt">启用总库存</span></td>
					</tr>
					<tr class="block">
						<th>总 库 存：</th>
						<td><input type="text" <?=$p_info['storage']==-1?"disabled":""?> class="textT" id="total_storage" value="<?=$p_info['storage']==-1?"":$p_info['storage']?>"/></td>
					</tr>
					<tr class="block">
						<th>启用日期：</th>
						<td><input type="text" <?=$p_info['storage']==-1?"disabled":""?> class="inputTime" id="storage_open" onmouseup="SelectDate(this,'yyyy-MM-dd',0,0)" value="<?=$p_info['storage_open']?>" name="opentime"/></td>
					</tr>
					<tr class="block">
						<th>已用库存：</th>
						<td><span id="sales"><?=$sales?></span></td>
					</tr>
                    <?php if( $p_info['sync_status']==2 && $p_info['sync_id'] ):?>
                        <tr class="block"><td colspan="2">同步产品禁止编辑</td></tr>
                    <?php else:?>
					<tr class="block">
						<th><input type="submit" class="btn" id="applyBtn" value="设置"/></th>
						<td><input type="reset" class="btn" value="重置"/></td>
					</tr>
                    <?php endif;?>
				</table>
				<p>
					1,未启用总库存，默认库存不限</br>
					2,已用库存从库存启用日期起统计。
				</p>
			</form>
		</div>
		<div class="clear"></div>
	</div>
<script type="text/javascript" src="js/ui/storageCal.js"></script>
<script type="text/javascript" src="js/json2.js"></script>
<script>
	(function(){
		storageCal.init.calDiv=$("#storageCal").get(0);
		var pid=parseInt($("#pid").val());
		var yearmonth=$("#begintime").val().substr(0,7);
		storageCal.show(yearmonth,pid,$("#begintime").val());
	})();
	$(".block").hide();
	$("#applyCheck").click(function(){
		if(this.checked==true){
			$("#total_storage").get(0).disabled=false;
			$("#storage_open").get(0).disabled=false;
            $(".block").show();
		}else{
			$("#total_storage").get(0).disabled=true;
			$("#storage_open").get(0).disabled=true;
            $(".block").hide();
		}
	});
	
	$("#applyBtn").click(function(){
		var params={"pid":$("#pid").val(),"tid":$("#tid").val(),"storage_open":$("#storage_open").val(),"total_storage":$("#total_storage").val()};
		if($("#applyCheck").get(0).checked==false){
			params['total_storage']=-1;
		}
		$.ajax({url:"daily_storage.php",
				async:false,
				type:"POST",
				dataType:"json",
				data:params,
				success:function(data){
					if(data.errcode==100){
						$("#sales").html(data.sales);
						alert("配置成功");
					}else{
						alert("配置失败");
					}
				}
			})
		return false;
	});
	
	$("#configBtn").click(function(){
		var daystorage=$("input#daystorage").val();
		if(daystorage==="") daystorage="-1";
		var sprice=$("#sprice").val();
		var lprice=$("#lprice").val();
		if(isNaN(sprice)||sprice<=0){
			alert("供货价请填写大于0的数字");
			return false;
		}
		if(isNaN(lprice)||lprice<=0){
			alert("零售价请填写大于0的数字");
			return false;
		}
		var hasChecked=0;
		var data=[];
		var json={};
		$("#storageCal tbody td input").each(function(){
			if(this.checked==false) return;
			hasChecked=1;
			var record={};
			var detail=$(this).parent().parent();
			record['st']=detail.attr("date");
			record['et']=detail.attr("date");
			record['rid']=detail.attr("uid");
			record['p1']=sprice;
			record['p2']=lprice;
			record['ptype']=1;
			record['storage']=daystorage;
			data.push(record);
		})
		if(hasChecked==0){
			alert("请选择配置日期");
			return false;
		}
		json['params']=JSON.stringify(data)
		json['pid']=$("#pid").val();
		$.ajax({
				url:"daily_storage.php",
				async:true,
				type:"POST",
				dataType:"json",
				data:json,
				success:function(json){
					if(json.errcode==100){
						var yearmonth=$("#storageCalContent input.yearmonth").first().val();
						delete(storageCal.data[yearmonth])
						storageCal.show(yearmonth,$("#pid").val(),$("#begintime").val());
						alert("设置成功");
					}else{
						alert(json.msg);
					}
				}
			})
		return false;
	})
	$("#delBtn").click(function(){
		var hasChecked = 0, data=[], json={};
		
		$("#storageCal tbody td input").each(function(){
			if(this.checked==false) return;
			hasChecked=1;
			var record={};
			var detail=$(this).parent().parent();
			record['st']=detail.attr("date");
			record['et']=detail.attr("date");
			record['rid']=detail.attr("uid");
			record['ptype']=1;
			data.push(record);
		})
		
		if(hasChecked==0){
			alert("请选择删除日期");
			return false;
		}
		
		json['params']=JSON.stringify(data);
		json['pid'] = $("#pid").val();
		json['act'] = 'clear';
		$.ajax({
				url:"daily_storage.php",
				async:true,
				type:"POST",
				dataType:"json",
				data:json,
				success:function(json){
					if(json.errcode==100){
						var yearmonth=$("#storageCalContent input.yearmonth").first().val();
						delete(storageCal.data[yearmonth])
						storageCal.show(yearmonth,$("#pid").val(),$("#begintime").val());
						alert("设置成功");
					}else{
						alert(json.msg);
					}
				}
			})
		return false;
	})
</script>	